﻿using System.Diagnostics;
using System.ServiceProcess;
using System.Timers;

namespace Event.Service
{
    partial class MainService : ServiceBase
    {
        private EventHandler _handler;
        private Timer _timerMain;

        public MainService()
        {
            InitializeComponent();

            CanPauseAndContinue = false;
            CanHandlePowerEvent = false;

            _timerMain = new Timer();
            _timerMain.Elapsed += TimerMainElapsed;

            _handler = new EventHandler();
        }

        [Conditional("DEBUG")]
        internal void DebugStart()
        {
            OnStart(new string[] { });
        }

        protected override void OnStart(string[] args)
        {
            _timerMain.Interval = 5000;
            _timerMain.AutoReset = true;
            _timerMain.Start();
        }

        [Conditional("DEBUG")]
        internal void DebugStop()
        {
            OnStop();
        }

        protected override void OnStop()
        {
            _timerMain.Stop();
        }

        private void TimerMainElapsed(object sender, ElapsedEventArgs e)
        {
            _timerMain.Stop();

            _handler.ProcessMessages();            

            _timerMain.Start();
        }
    }
}